package com.tenement.controller.homepage;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tenement.mybatis.entity.House;
import com.tenement.service.interfaces.IHouseService;

/**
 * 首页
 * 
 * @author wk
 *
 */
@Controller
public class HomePageController {

	private static final Log logger = LogFactory.getLog(HomePageController.class);

	@Autowired
	private IHouseService iHouseService;

	/**
	 * 网站首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/index.action")
	public String to_index() {
		return "redirect:view/homePage.html";
	}

	/**
	 * 未使用
	 * 
	 * 模糊查询,主页搜索框,
	 * 
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/getHouseByLike.action")
	public @ResponseBody JSONObject getHouseByLike(@RequestBody String condition) {
		logger.info("【主页模糊查询，参数condition】" + condition);
		if (condition == null || "".equals(condition)) {
			try {
				throw new Exception("【参数condition为空】");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return iHouseService.getHouseByLike(condition);
	}

	/**
	 * 未使用
	 * 
	 * 获取所有的房源信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getHouseAll.action")
	public @ResponseBody List<House> getHouseAll() {
		return null;
	}

	/**
	 * 条件查询房源信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryHouse.action")
	public @ResponseBody JSONObject queryHouse(@RequestBody String jsonData) {
		
		JSONObject jsonstr = JSON.parseObject(jsonData);
		String h_area = jsonstr.getString("h_area");
		String h_region = jsonstr.getString("h_region");
		String h_rent = jsonstr.getString("h_rent");
		String h_room = jsonstr.getString("h_room");
		String h_heading = jsonstr.getString("h_heading");
		String h_decoration = jsonstr.getString("h_decoration");
		String h_rental_method = jsonstr.getString("h_rental_method");
		String sort = jsonstr.getString("sort");// 租金排序 desc asc
		String pageIndex = jsonstr.getString("pageIndex");
		String condition = jsonstr.getString("condition");
		logger.info("【主页查询功能】查询条件condition: "+condition);
		if (h_room == null) {
			h_room = "0";
		}
		if (h_decoration == null) {
			h_decoration = "0";
		}
		if (pageIndex == null) {
			pageIndex = "1";
		}
		if (sort == null) {
			sort = "asc".trim();
		}
		House house = new House(h_area, h_region, h_rent, Integer.parseInt(h_room), h_heading,
				Integer.parseInt(h_decoration), h_rental_method);
		return iHouseService.queryHouseBy7(house, sort, pageIndex, condition);
	}

}
